<?php defined('RUN_KATANA') or die('Restricted access'); 
if($shop->getOrders())
{
	$sum['items'] = 0;
	$sum['items_vat'] = 0;
	$sum['items_total'] = 0;
	$sum['shipping'] = 0;
	$sum['order_total'] = 0;
	$out = '';
	
	$dateStart = mktime(0,0,0,date('m'),date('d'),date('Y'));
	$dateEnd = mktime(0,0,0,date('m'),date('t'),date('Y'));
	
	if($_REQUEST['date_start'] != '')
	{ 
		$year = $shop->dateTimeExtract($_REQUEST['date_start'], 'year');
		$month = $shop->dateTimeExtract($_REQUEST['date_start'], 'month');
		$day = $shop->dateTimeExtract($_REQUEST['date_start'], 'day');
		if(($day > 0) && ($month > 0) && ($year > 0))
		{ $dateStart = mktime(0,0,0,$month,$day,$year); }
	}
	if($_REQUEST['date_end'] != '')
	{ 
		$year = $shop->dateTimeExtract($_REQUEST['date_end'], 'year');
		$month = $shop->dateTimeExtract($_REQUEST['date_end'], 'month');
		$day = $shop->dateTimeExtract($_REQUEST['date_end'], 'day');
		if(($day > 0) && ($month > 0) && ($year > 0))
		{ $dateEnd = mktime(0,0,0,$month,$day,$year); }
	}
	
	foreach($shop->getOrders() as $order)
	{
		global $txt;
		
		$year = $order->dateTimeExtract($order->getDateDelivery(), 'year');
		$month = $order->dateTimeExtract($order->getDateDelivery(), 'month');
		$day = $order->dateTimeExtract($order->getDateDelivery(), 'day');
		if(($day > 0) && ($month > 0) && ($year > 0))
		{ $dateOrder = mktime(0,0,0,$month,$day,$year); }
		
		$display = true;
		if(($dateStart > $dateOrder) || ($dateOrder > $dateEnd))
		{ $display = false; }
		if(($_REQUEST['shipping-method'] > 0) && ($_REQUEST['shipping-method'] != $order->getShippingId()))
		{ $display = false; }
		if($display == true)
		{
			$items = $order->getOrderItems();
			if($items > 0)
			{
				foreach($items as $item)
				{
					$out .= '<tr class="items">';
						$out .= '<td class="first center">'.substr($order->getDateDelivery(), 0, 10).'</td>';
						$out .= '<td>';
						$out .= $order->getId();
						$out .= '</td>';
						$out .= '<td>'.intval($order->getShippingZoneCode()).'</td>';
						$out .= '<td>'.$order->getShippingZoneTitle().'</td>';
						$out .= '<td>'.$item->getItemIdentity().'</td>';
						$out .= '<td class="right">'.number_format($item->getSingleItemPriceTotal(1),2,',',' ').'</td>';
						$out .= '<td class="last right">'.$item->getCount().'</td>';
					$out .= '</tr>';
				}
				if($order->getCostShipping())
				{
					$out .= '<tr class="items">';
						$out .= '<td class="first center">'.substr($order->getDateDelivery(), 0, 10).'</td>';
						$out .= '<td>';
						$out .= $order->getId();
						$out .= '</td>';
						$out .= '<td>'.intval($order->getShippingZoneCode()).'</td>';
						$out .= '<td>'.ucfirst($txt->getWord('shipping')).': '.$order->getDeliveryCity().'</td>';
						$out .= '<td>'.$order->getShippingID().'</td>';
						$out .= '<td class="right">'.number_format($order->getCostShipping(2),2,',',' ').'</td>';
						$out .= '<td class="last right">1</td>';
					$out .= '</tr>';
				}
			}
		}
	}
	
	if(OUTPUT_FORMAT == 'csv')
	{

		$headers  = ucfirst($txt->getWord('delivery_date')).';';
		$headers .= ucfirst($txt->getWord('order_id')).';';
		$headers .= ucfirst($txt->getWord('shipping').' '.$txt->getWord('zone')).';';
		$headers .= ucfirst($txt->getWord('shipping').' '.$txt->getWord('zone').' '.$txt->getWord('title')).';';
		$headers .= ucfirst($txt->getWord('identity')).';';
		$headers .= ucfirst($txt->getWord('std_price').' ('.$txt->getWord('inc').'. '.$txt->getWord('tax').');');
		$headers .= ucfirst($txt->getWord('count')).';';

		$out = str_replace('<tr class="items">','',$out);
		$out = str_replace('<tr>','',$out);
		$out = str_replace('<td>','',$out);
		$out = str_replace('</td>',';',$out);
		$out = str_replace('<td class="first center">','',$out);
		$out = str_replace('<td class="last right">','',$out);
		$out = str_replace('<td class="right">','',$out);
		
		$out = str_replace('&Oslash;','O',$out);
		$out = str_replace('&oslash;','o',$out);
		$out = str_replace('&Aring;','A',$out);
		$out = str_replace('&aring;','a',$out);
		$out = str_replace('&AElig;','AE',$out);
		$out = str_replace('&aelig;','ae',$out);
		
		$csvLines = explode("</tr>",$out);
		if(count($csvLines > 0))
		{
			$filename = "report_".$_GET['report'].date("Y-m-d_H-i").'.csv';
			$file = fopen(FILES_PATH.'reports/'.$filename,"w");
			
			fputcsv($file, explode(";",$headers), ';');
			
			foreach($csvLines as $line)
			{
				$csvLine = explode(";",$line);
				fputcsv($file, $csvLine, ';');
			}
			
			fclose($file);
			$out = '<a target="_blank" href="'.FILES_URL.'reports/'.$filename.'">'.ucfirst($txt->getWord('download')).' '.$filename.'</a>';
		}
	}
	echo $out;
}
?>